package com.jinlejia.erp.ui.common;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.jinlejia.erp.R;

import java.util.concurrent.TimeUnit;

public class ADActivity  extends AppCompatActivity {

    String flag;
    Handler handler = new Handler();
    Runnable runnable;
    long showSeconds; //要显示的描述
    String adUrl; //AD广告
    String detailUrl;
    private ADActivity adActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);
        Intent intent = getIntent();
        flag = intent.getStringExtra("flag");
        adUrl = intent.getStringExtra("adUrl");
        detailUrl = intent.getStringExtra("detailUrl");
        showSeconds = intent.getLongExtra("showSeconds",0);
        showSeconds = TimeUnit.SECONDS.toMillis(showSeconds);
        adActivity = this;
        findViewById(R.id.adImageView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adActivity!=null && !adActivity.isFinishing()){
                    if (TextUtils.isEmpty(detailUrl))return; //如果没有详情广告页，则返回
                    handler.removeCallbacks(runnable);
                    Intent intent = new Intent();
                    intent.setClass(adActivity,ADInfoActivity.class);
                    intent.putExtra("flag",flag);
                    intent.putExtra("detailUrl",detailUrl);
                    startActivity(intent);
                    finish();
                }

            }
        });
        findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消定时任务
                handler.removeCallbacks(runnable);
                goTo();
            }
        });
        ImageView imageView = findViewById(R.id.adImageView);
        Glide.with(this).load(adUrl)
                .placeholder(R.mipmap.wellcome)
                .into(imageView)
        ;
        runnable = new Runnable() {
            @Override
            public void run() {
                goTo();
            }
        };
        handler.postDelayed(runnable, showSeconds);
    }

    void goTo() {
        if (flag.equals("main")) {
            startActivity(new Intent(this, MainActivity.class));
        } else {
            startActivity(new Intent(this, LoginActivity.class));
        }
        finish();
    }
}
